package tool

import (
	"didi-vehicle-certification/api/global"
	"fmt"
	"github.com/google/uuid"
	"github.com/hashicorp/consul/api"
	"go.uber.org/zap"
)

type Consul struct {
	Host string
	Port int
}

func (c *Consul) NewConsul(host string, port int) *Consul {
	return &Consul{
		Host: host,
		Port: port,
	}
}
func (c *Consul) ConsulRegister(address string, port int, name string, tags []string) {
	registration := new(api.AgentServiceRegistration)
	registration.Address = address
	registration.Port = port
	registration.Name = name
	registration.Tags = tags
	registration.ID = uuid.NewString()
	err := global.ConsulClient.Agent().ServiceRegister(registration)
	if err != nil {
		zap.S().Errorf("consul注册失败")
	}
}
func (c *Consul) ConsulFilter(name string) (map[string]*api.AgentService, error) {
	filter := fmt.Sprintf("Service == `%s`", name)
	withFilter, err := global.ConsulClient.Agent().ServicesWithFilter(filter)
	if err != nil {
		return nil, nil
	}
	return withFilter, err
}
